package com.vote.web.controller.business;

import com.vote.business.domain.UserVoteRecord;
import com.vote.business.domain.vo.UserVoteRecordVO;
import com.vote.business.service.IUserVoteRecordService;
import com.vote.common.annotation.Log;
import com.vote.common.core.controller.BaseController;
import com.vote.common.core.domain.AjaxResult;
import com.vote.common.core.domain.entity.SysUser;
import com.vote.common.core.domain.model.LoginUser;
import com.vote.common.core.page.TableDataInfo;
import com.vote.common.enums.BusinessType;
import com.vote.common.utils.SecurityUtils;
import com.vote.common.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 用户投票记录Controller
 * 
 * @author ruoyi
 * @date 2025-07-14
 */
@RestController
@RequestMapping("/business/voteRecord")
public class UserVoteRecordController extends BaseController
{
    @Autowired
    private IUserVoteRecordService userVoteRecordService;

    /**
     * 查询用户投票记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:voteRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserVoteRecord userVoteRecord)
    {
        startPage();
        List<UserVoteRecord> list = userVoteRecordService.selectUserVoteRecordList(userVoteRecord);
        return getDataTable(list);
    }
    /**
     * 查询用户投票记录列表
     */
    @PreAuthorize("@ss.hasPermi('vote:voteRecord:list')")
    @RequestMapping("/listVoteRecordDto")
    public TableDataInfo list(UserVoteRecordVO record) {
        startPage();
        List<UserVoteRecordVO> list = userVoteRecordService.selectUserVoteRecordDtoList(record);
        return getDataTable(list);
    }
    /**
     * 导出用户投票记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:voteRecord:export')")
    @Log(title = "用户投票记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserVoteRecord userVoteRecord)
    {
        List<UserVoteRecord> list = userVoteRecordService.selectUserVoteRecordList(userVoteRecord);
        ExcelUtil<UserVoteRecord> util = new ExcelUtil<UserVoteRecord>(UserVoteRecord.class);
        util.exportExcel(response, list, "用户投票记录数据");
    }

    /**
     * 获取用户投票记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:voteRecord:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(userVoteRecordService.selectUserVoteRecordByRecordId(recordId));
    }
    /**
     * 获取用户投票记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:voteRecord:query')")
    @GetMapping(value = "/dto/{recordId}")
    public AjaxResult selectUserVoteRecordDtoById(@PathVariable("recordId") Long recordId)
    {
        return success(userVoteRecordService.selectUserVoteRecordDtoById(recordId));
    }

    /**
     * 新增用户投票记录
     */
    @PreAuthorize("@ss.hasPermi('business:voteRecord:add')")
    @Log(title = "用户投票记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserVoteRecord userVoteRecord)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        userVoteRecord.setUserId(user.getUserId());
        userVoteRecord.setVoteTime(new Date());
        return userVoteRecordService.insertUserVoteRecord(userVoteRecord);
    }

    /**
     * 修改用户投票记录
     */
    @PreAuthorize("@ss.hasPermi('business:voteRecord:edit')")
    @Log(title = "用户投票记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserVoteRecord userVoteRecord)
    {
        return toAjax(userVoteRecordService.updateUserVoteRecord(userVoteRecord));
    }

    /**
     * 删除用户投票记录
     */
    @PreAuthorize("@ss.hasPermi('business:voteRecord:remove')")
    @Log(title = "用户投票记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(userVoteRecordService.deleteUserVoteRecordByRecordIds(recordIds));
    }
}
